package com.ruiysoft.service.logistics;

import com.ruiysoft.entity.logistics.LogisticsShopApiConf;
import com.ruiysoft.entity.logistics.NoSuchExpressCallerException;
import com.ruiysoft.mapper.logistics.LogisticsShopApiConfMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ExpressCollerFactory {

    private final Map<String,IExpressCaller> expressCallerMap = new HashMap<>();

    private Map<Integer,List<LogisticsShopApiConf>> listMap = new HashMap<>();

    /*private ILogisticsCompanyBaseService iLogisticsCompanyBaseService;

    private ILogisticsShopApiConfService iLogisticsShopApiConfService;

    private LogisticsShopApiConfMapper logisticsShopApiConfMapper;*/

    private ExpressCollerFactory(){
    }

    private static class ExpressCollerFactoryHandler{
        private static ExpressCollerFactory expressCollerFactory = new ExpressCollerFactory();
    }

    public final ExpressCollerFactory registerColler(List<IExpressCaller> expressCallers){
        synchronized (expressCallerMap) {
            if(expressCallerMap.size() == 0) {
                expressCallers.forEach(c -> expressCallerMap.put(c.getAPISupplier(), c));
            }
        }
        return this;
    }

    /*public static ExpressCollerFactory initFactoryDependency(
            ILogisticsCompanyBaseService iLogisticsCompanyBaseService,
            ILogisticsShopApiConfService iLogisticsShopApiConfService,
            LogisticsShopApiConfMapper logisticsShopApiConfMapper
    ){
        ExpressCollerFactoryHandler.expressCollerFactory.iLogisticsCompanyBaseService = iLogisticsCompanyBaseService;
        ExpressCollerFactoryHandler.expressCollerFactory.iLogisticsShopApiConfService = iLogisticsShopApiConfService;
        ExpressCollerFactoryHandler.expressCollerFactory.logisticsShopApiConfMapper = logisticsShopApiConfMapper;
        return ExpressCollerFactoryHandler.expressCollerFactory;
    }*/

    public static ExpressCollerFactory getInstance(){
        return ExpressCollerFactoryHandler.expressCollerFactory;
    }


    public IExpressCaller getExpressCaller(String apiSupplier) throws NoSuchExpressCallerException {
        return Optional.ofNullable(expressCallerMap.get(apiSupplier)).orElseThrow(NoSuchExpressCallerException::new);
    }

}
